package com.cn.qgx428.lazydiary.ui.activity

import android.app.DatePickerDialog
import android.graphics.BitmapFactory
import android.graphics.Color
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.widget.DatePicker
import com.cn.qgx428.lazydiary.Presenter.PlanPresenter
import com.cn.qgx428.lazydiary.R
import com.cn.qgx428.lazydiary.Utils.FileUtil
import com.cn.qgx428.lazydiary.Utils.MConst
import com.cn.qgx428.lazydiary.Utils.PhotoChoseActivity
import com.cn.qgx428.lazydiary.Utils.SPUtil
import com.cn.qgx428.lazydiary.extension.getCurrenDayOfYear
import com.cn.qgx428.lazydiary.extension.getCurrenTimePrecise
import com.cn.qgx428.lazydiary.local.LAMessege
import com.cn.qgx428.lazydiary.local.LAction
import com.cn.qgx428.lazydiary.local.LActionWacher
import com.cn.qgx428.lazydiary.local.LManager
import com.cn.qgx428.lazydiary.ui.activity.subActivity.PlanModifyActivity
import kotlinx.android.synthetic.main.activity_plan_create.*
import org.jetbrains.anko.startActivity
import org.jetbrains.anko.toast
import java.util.*

class PlanCreateActivity : BaseActivity(), DatePickerDialog.OnDateSetListener, LActionWacher {

    var endTime = ""
    var imageName = MConst.PLAN_DEFAULT_IMG
    var imagePath = ""

    override fun initUI() {
        hindActionBar()
        setStatuBarColor(Color.BLACK)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_plan_create)
        initView()
    }

    private fun initView() {
        planCreate_back.setOnClickListener { finish() }
        planCreate_timeLayout.setOnClickListener({
            val dialog = getDatePickerDialog(this)
            dialog.datePicker.minDate = System.currentTimeMillis()
            dialog.show()
        })

        planCreate_commit.setOnClickListener({
            createPlan()
        })

        planCreate_img.setOnClickListener {
            startActivity<PhotoChoseActivity>()
            LManager.newInstance().register(this, LAction.ACTION_GET_LOCAL_IMAGE)
        }
    }

    private fun createPlan() {
        // 检查
        if (endTime.equals(""))
            toast("请选择计划结束时间")
        else if (planCreate_title.text.toString().length < 1)
            toast("起个计划名哦")
        else
            PlanPresenter.createPlan(SPUtil.currntUserId().toInt(), planCreate_title.text.toString(),
                    Calendar.getInstance().getCurrenDayOfYear(), endTime, imageName, imagePath,listener = {
                startActivity<PlanModifyActivity>(
                        MConst.PLAN_ID to it
                )
                // 通知更新
                LManager.newInstance().trigger(LAction.ACTION_CREATE_PLAN)
                finish()
            });
    }

    override fun onDateSet(view: DatePicker?, year: Int, month: Int, dayOfMonth: Int) {
        endTime = "${year.toString()}年${month.toString()}月${dayOfMonth.toString()}日"
        planCreate_endTime.text = endTime
    }

    override fun onTrigger(action: String, msg: LAMessege) {
        imagePath = msg.arg1
        planCreate_img.setImageBitmap(BitmapFactory.decodeFile(msg.arg1))
        imageName = FileUtil.getFileName(msg.arg1)
    }
}
